package com.dhcc.sds.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.dao.SdsQceitemDicMapper;
import com.dhcc.sds.base.dao.SdsQcfitemDicMapper;
import com.dhcc.sds.base.dao.SdsQcformItemMapper;
import com.dhcc.sds.base.entity.SdsQceitemDicEntity;
import com.dhcc.sds.base.entity.SdsQcfitemDicEntity;
import com.dhcc.sds.base.entity.SdsQcformItemEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sds.base.dao.SdsQcentityItemMapper;
import com.dhcc.sds.base.service.ISdsQcentityItemService;
import com.dhcc.sds.base.entity.SdsQcentityItemEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * 质控项目表表数据服务层接口
 *
 * @author sunyingjie
 * @ClassName: SdsQcentityItemServiceImpl
 * @Description: TODO
 * @date 2021-03-22 10:10:50
 */
@Service("sdsQcentityItemService")
public class SdsQcentityItemServiceImpl extends BaseServiceImpl<SdsQcentityItemMapper, SdsQcentityItemEntity> implements ISdsQcentityItemService {

    @Autowired
    private SdsQcformItemMapper sdsQcformItemMapper;
    @Autowired
    private SdsQceitemDicMapper sdsQceitemDicMapper;
    @Autowired
    private SdsQcfitemDicMapper sdsQcfitemDicMapper;

    @Override
    public List<SdsQcentityItemEntity> page(Page<SdsQcentityItemEntity> page, Map<String, Object> map) {
        if ("0".equals(map.get("entityId"))) {
            return new ArrayList<>();
        }
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<SdsQcentityItemEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void synchro(Map<String, Object> params) {
        Wrapper<SdsQcentityItemEntity> wrapper = new EntityWrapper<>();
        if (!CommonUtil.isEmpty(params.get("entityItemId"))) {
            wrapper.eq("id", params.get("entityItemId"));
        }
        wrapper.eq("entity_id", params.get("entityId"));
        wrapper.eq("update_flag", 1);
        List<SdsQcentityItemEntity> qcEntityList = this.baseMapper.selectList(wrapper);
        for (SdsQcentityItemEntity entity : qcEntityList) {
            this.synchroFormItem(entity);
            this.synchroFormItemDic(entity);
        }
    }

    /**
     * 同步项目信息
     *
     * @param qcEntity
     */
    private void synchroFormItem(SdsQcentityItemEntity qcEntity) {
        SdsQcformItemEntity qcForm = new SdsQcformItemEntity();
        qcForm.setEntityId(qcEntity.getEntityId());
        qcForm.setBtCode(qcEntity.getFormItemCode());
        qcForm = sdsQcformItemMapper.selectOne(qcForm);
        // 如果已关联表单项目不存在就新增，存在则更新表单并将值域全部暂时置为无效
        if (CommonUtil.isOneEmpty(qcForm)) {
            SdsQcformItemEntity newQcForm = new SdsQcformItemEntity();
            newQcForm.setEntityId(qcEntity.getEntityId());
            newQcForm.setBtCode(qcEntity.getBtCode());
            newQcForm.setBtDesc(qcEntity.getBtDesc());
            newQcForm.setBtIsNeeded(qcEntity.getBtIsNeeded());
            newQcForm.setBtIsActive(qcEntity.getBtIsActive());
            newQcForm.setUpdateFlag(1);
            newQcForm.insert();
        } else {
            qcForm.setBtCode(qcEntity.getBtCode());
            qcForm.setBtDesc(qcEntity.getBtDesc());
            qcForm.setBtIsActive(qcEntity.getBtIsActive());
            qcForm.setBtIsNeeded(qcEntity.getBtIsNeeded());
            qcForm.setUpdateFlag(1);
            qcForm.updateAllColumnById();
        }
    }

    /**
     * 同步值域信息
     *
     * @param qcEntity
     */
    private void synchroFormItemDic(SdsQcentityItemEntity qcEntity) {
        // 查询标准项目值域
        Map<String, Object> params = new HashMap<>();
        params.put("entityItemId", qcEntity.getId());
        List<SdsQceitemDicEntity> qcEItemDics = sdsQceitemDicMapper.list(params);
        // 查询表单项目
        SdsQcformItemEntity qcForm = new SdsQcformItemEntity();
        qcForm.setEntityId(qcEntity.getEntityId());
        qcForm.setBtCode(qcEntity.getFormItemCode());
        qcForm = sdsQcformItemMapper.selectOne(qcForm);
        // 同步前先将关联表单值域置为无效
        Wrapper<SdsQcfitemDicEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("form_item_id", qcForm.getId());
        sdsQcfitemDicMapper.updateForSet("is_active=0", wrapper);
        // 查询表单项目值域
        params.clear();
        params.put("formItemId", qcForm.getId());
        List<SdsQcfitemDicEntity> qcFItemDics = sdsQcfitemDicMapper.list(params);
        outer:for (SdsQceitemDicEntity qcEItemDic : qcEItemDics) {
            for (SdsQcfitemDicEntity qcFItemDic : qcFItemDics) {
                // 如果标准项目值域代码与表单项目值域代码相同，则同步数据并遍历下一条标准项目值域数据
                if (qcEItemDic.getBtCode().equals(qcFItemDic.getCode())) {
                    // 同步描述和有效状态
                    qcFItemDic.setDesc(qcEItemDic.getBtDesc());
                    qcFItemDic.setIsActive(qcEItemDic.getIsActive());
                    // 更新
                    qcFItemDic.updateAllColumnById();
                    continue outer;
                }
            }
            // 上面表单值域数据遍历完没有相同代码，则新增一条表单值域数据
            SdsQcfitemDicEntity newFItemDic = new SdsQcfitemDicEntity();
            newFItemDic.setFormItemId(qcForm.getId());
            newFItemDic.setCode(qcEItemDic.getBtCode());
            newFItemDic.setDesc(qcEItemDic.getBtDesc());
            newFItemDic.setIsActive(qcEItemDic.getIsActive());
            newFItemDic.insert();
        }
    }
}